<template>
  <div class="role-management-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit('')">
          添加
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      default-expand-all
      row-key="menu_path"
      :tree-props="{ children: 'sub_menu', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="菜单ID" prop="id" show-overflow-tooltip />
      <el-table-column
        label="菜单名称"
        prop="menu_name"
        show-overflow-tooltip
      />
      <el-table-column label="URL路径" prop="menu_path" show-overflow-tooltip />
      <el-table-column
        label="文件路径"
        prop="menu_page"
        show-overflow-tooltip
      />
      <el-table-column label="菜单状态" prop="menu_status" width="150px">
        <template #default="{ row }">
          <el-switch
            v-model="row.menu_status"
            active-text="显示"
            :active-value="1"
            inactive-text="隐藏"
            :inactive-value="0"
            @change="switchChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="图标" show-overflow-tooltip width="100px">
        <template #default="{ row }">
          <vab-icon v-if="row.menu_icon" :icon="row.menu_icon" />
        </template>
      </el-table-column>
      <el-table-column
        label="排序"
        prop="menu_sort"
        show-overflow-tooltip
        width="100px"
      />
      <el-table-column label="操作" show-overflow-tooltip width="85">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.perpage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
import { doDelete, getTree, doSwitch } from '@/api/rule'
import Edit from './components/menuEdit'

export default {
  name: 'Menus',
  components: { Edit },
  data() {
    return {
      list: [],
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      queryForm: {
        page: 1,
        perpage: 20,
      },
      switchData: {
        menu_id: '',
        status_name: 'menu_status',
        status_value: '',
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleEdit(row) {
      if (row) {
        this.$refs['edit'].showEdit(row)
      } else {
        this.$refs['edit'].showEdit()
      }
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { data } = await doDelete({ menu_ids: row.id })
          if (data.is_success) {
            this.$baseMessage('删除成功', 'success', 'vab-hey-message-success')
            await this.fetchData()
          }
        })
      }
    },
    async switchChange(row) {
      this.switchData.menu_id = row.id
      this.switchData.status_value = row.menu_status
      await doSwitch(this.switchData)
      const {
        data: { items, total },
      } = await getTree({})
      this.list = items
      this.total = total
    },
    async fetchData() {
      this.listLoading = true
      const {
        data: { items, total },
      } = await getTree({})
      this.list = items
      this.total = total
      this.listLoading = false
    },
    handleSizeChange(val) {
      this.queryForm.perpage = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.page = val
      this.fetchData()
    },
    handleNodeClick({ role }) {
      this.fetchData(role)
    },
  },
}
</script>

<style lang="scss" scoped>
$base: '.menu-management';
#{$base}-container {
  padding: 0 !important;
  background: $base-color-background !important;
}
</style>
